/**
 * Copyright By Nanjing Fujitsu Nanda Software Technology Co., Ltd
 * 下午4:45:36
 * LoginSuccessHandler.java
 * 
 */
package com.fujitsu.nanjing.tieba.security;

import java.io.IOException;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.stereotype.Component;

import com.fujitsu.nanjing.tieba.ibator.CoreUser;
import com.fujitsu.nanjing.tieba.service.UserService;

/**
 * @author Administrator
 *
 */
/**
 * 跳转到登录前页面，并且处理相关session
 * **/
@Component
public class LoginSuccessHandler
		extends
			SavedRequestAwareAuthenticationSuccessHandler {
	/* private static final String LoginPath = "/login"; */
	private final String LASTACCESS = "lastAccess";
	private final String LOGIN = "login";
	protected final String CURRENTUSER = "currentUser";
	protected final String USERID = "userId";
	@Resource
	private UserService userService;
	@Override
	public void onAuthenticationSuccess(HttpServletRequest httpservletrequest,
			HttpServletResponse httpservletresponse,
			Authentication authentication) throws IOException, ServletException {
		UserDetails userDetails = (UserDetails) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		CoreUser user = userService.findById(userDetails.getUsername());
		HttpSession session = httpservletrequest.getSession();
		session.setAttribute(LOGIN, "true");
		session.setAttribute(CURRENTUSER, user.getNickname());
		session.setAttribute(USERID, user.getUserid());
		session.setAttribute(LASTACCESS, user.getLastvisited());
		user.setLastvisited(new Date());
		user.setLogincount((byte) 1);
		userService.update(user);
		Object savedRequestObject = httpservletrequest.getSession()
				.getAttribute("SPRING_SECURITY_SAVED_REQUEST");
		if (savedRequestObject != null) {
			String redirectUrl = ((SavedRequest) savedRequestObject)
					.getRedirectUrl();
			httpservletrequest.getSession().removeAttribute(
					"SPRING_SECURITY_SAVED_REQUEST");
			httpservletresponse.sendRedirect(redirectUrl);
		}
		super.onAuthenticationSuccess(httpservletrequest, httpservletresponse,
				authentication);
		/*
		 * String targetUrl = null;
		 * 
		 * String url = httpservletrequest.getRequestURL().toString(); //
		 * 取得登陆前的url String refererUrl =
		 * httpservletrequest.getHeader("Referer");
		 * 
		 * String[] args = url.split("//"); String rootUrl = args[0] + "//" +
		 * args[1].split("/")[0]; if (refererUrl.startsWith(rootUrl) &&
		 * !refererUrl.startsWith(rootUrl + LoginPath)) { targetUrl =
		 * refererUrl; } else { targetUrl = rootUrl; }
		 * httpservletresponse.sendRedirect(targetUrl);
		 */

	}

}
